#include <iostream>
#include <string>
#include <pthread.h>
#include "Mutex.hpp"
#include "Cond.hpp"
using namespace CondModule;
bool ready = false;
Mutex mutex;
Cond cond;
int ticket = 0;
void *Consumer(void *args)
{
    std::string name = static_cast<const char *>(args);
    mutex.Lock();
    while (!ready)
    {
        std::cout << name << "资源等待中" << std::endl;
        cond.Wait(mutex);
    }
    // 处理资源
    std::cout << name << "资源成功消耗" << std::endl;
    mutex.Unlock();
    return nullptr;
}
void *Producer(void *args)
{
    std::string name = static_cast<const char *>(args);
    // 准备资源
    ready = true;
    std::cout << name << "资源准备完毕" << std::endl;
    cond.Notify();
    return nullptr;
}
int main()
{
    pthread_t consumer, producer;
    pthread_create(&consumer, nullptr, Consumer, (void *)"consumer");
    pthread_create(&producer, nullptr, Producer, (void *)"prodecer");
    pthread_join(consumer, nullptr);
    pthread_join(producer, nullptr);
    return 0;
}